package golf.stockinfo.testutil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;

public class BuySellFutureSimulation {
	
	private static String weeklyFileName = "/Users/golf/Documents/temp/SET50-Weekly-19960617-20121028.txt";
	private static String dailyFileName = "/Users/golf/Documents/temp/SET50-Daily-19960617-20121028.txt";

	public static void main(String[] args) {

		try {
			
			ArrayList<String> buyDates = new ArrayList<String>();
			ArrayList<String> sellDates = new ArrayList<String>();
			ArrayList<Float> buyIndex = new ArrayList<Float>();
			ArrayList<Float> sellIndex = new ArrayList<Float>();
			float[] maxLoss = null;
			boolean continueLoop = true;
			
			
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(weeklyFileName);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// Read File Line By Line
			int i = 0;
			boolean wasSold = false;
			while ((strLine = br.readLine()) != null) {
				// Print the content on the console
				//System.out.println(strLine);
				if(i>9){

					while(strLine.indexOf("  ") > 0){
						strLine = strLine.replaceAll("  ", " ");
					}

					String[] lineItems = strLine.split(" ");
					String date = lineItems[0];
					float high = Float.parseFloat(lineItems[2].replaceAll(",",""));
					float low = Float.parseFloat(lineItems[3].replaceAll(",",""));
					float close = Float.parseFloat(lineItems[4].replaceAll(",",""));
					float close_smav10 = Float.parseFloat(lineItems[5].replaceAll(",",""));
					
					if(wasSold){
						if(low > close_smav10){
							System.out.println(";Buy:" + date + ";" + close);
							buyDates.add(date);
							buyIndex.add(close);
							wasSold = false;
						}
					}else{
						if(high < close_smav10){
							System.out.print("Sell:" + date + ";" + close);
							sellDates.add(date);
							sellIndex.add(close);
							wasSold = true;
						}
					}
					
				}
				i++;
			}
			
			// Close the input stream
			in.close();
			br.close();
			fstream.close();
			i = 0;
			System.out.println("");
			System.out.println("==================================================");
			
			fstream = new FileInputStream(dailyFileName);
			// Get the object of DataInputStream
			in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));
			
			if((strLine = br.readLine()) != null){
				
				//Initialize maxLoss
				maxLoss = new float[sellDates.size()];
				for(int txnIndx=0; txnIndx < sellDates.size(); txnIndx++){
					maxLoss[txnIndx] = 0.0f;
				}
				
				for(int txnIndx=0; txnIndx < buyDates.size(); txnIndx++){
					String sellDate = sellDates.get(txnIndx);
					float close = 0.0f;
					float gap = 0.0f;
					String[] lineItems = null;
					
					System.out.println("==================================================");
					
					//Find the start date, supposed Monday
					while ((strLine = br.readLine()) != null && strLine.indexOf(sellDate) < 0) {
						while(strLine.indexOf("  ") > 0){
							strLine = strLine.replaceAll("  ", " ");
						}
						lineItems = strLine.split(" ");
						close = Float.parseFloat(lineItems[4].replaceAll(",",""));
					}
					
					//But since we start on Friday, so find the Friday
					continueLoop = true;
					while(continueLoop){
						if(strLine != null){
							while(strLine.indexOf("  ") > 0){
								strLine = strLine.replaceAll("  ", " ");
							}
							lineItems = strLine.split(" ");
							close = Float.parseFloat(lineItems[4].replaceAll(",",""));
							
							if(close == sellIndex.get(txnIndx)){
								continueLoop = false;
							}
						}else{
							continueLoop = false;
						}
						
						if(continueLoop){
							strLine = br.readLine();
						}
					}
					
					while(strLine.indexOf("  ") > 0){
						strLine = strLine.replaceAll("  ", " ");
					}
					lineItems = strLine.split(" ");
					String date = lineItems[0];
					
					gap =  close - Float.parseFloat(lineItems[4].replaceAll(",",""));
					
					if((sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""))) < maxLoss[txnIndx])
						maxLoss[txnIndx] = sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""));
					close = Float.parseFloat(lineItems[4].replaceAll(",",""));
					System.out.println(date + ":" + close + ":" + gap);
					
					while ((strLine = br.readLine()) != null && strLine.indexOf(buyDates.get(txnIndx)) < 0) {
						while(strLine.indexOf("  ") > 0){
							strLine = strLine.replaceAll("  ", " ");
						}
						lineItems = strLine.split(" ");
						date = lineItems[0];
						gap =  close - Float.parseFloat(lineItems[4].replaceAll(",",""));
						close = Float.parseFloat(lineItems[4].replaceAll(",",""));
						System.out.println(date + ":" + close + ":" + gap);
						if((sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""))) < maxLoss[txnIndx])
							maxLoss[txnIndx] = sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""));
					}
					
					if(strLine.indexOf(buyDates.get(txnIndx)) == 0){
						while(strLine.indexOf("  ") > 0){
							strLine = strLine.replaceAll("  ", " ");
						}
						lineItems = strLine.split(" ");
						date = lineItems[0];
						gap =  close - Float.parseFloat(lineItems[4].replaceAll(",",""));
						close = Float.parseFloat(lineItems[4].replaceAll(",",""));
						System.out.println(date + ":" + close + ":" + gap);
						if((sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""))) < maxLoss[txnIndx])
							maxLoss[txnIndx] = sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""));
					}
					
					continueLoop = true;
					while(continueLoop){
						
						strLine = br.readLine();

						if(strLine != null){
							while(strLine.indexOf("  ") > 0){
								strLine = strLine.replaceAll("  ", " ");
							}
							lineItems = strLine.split(" ");
							date = lineItems[0];
							gap = close - Float.parseFloat(lineItems[4].replaceAll(",",""));
							close = Float.parseFloat(lineItems[4].replaceAll(",",""));
							System.out.println(date + ":" + close + ":" + gap);
							if((sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""))) < maxLoss[txnIndx])
								maxLoss[txnIndx] = sellIndex.get(txnIndx) - Float.parseFloat(lineItems[4].replaceAll(",",""));
							
							if(close == buyIndex.get(txnIndx)){
								continueLoop = false;
							}
						}else{
							continueLoop = false;
						}
					}
				}
			}

			in.close();
			br.close();
			fstream.close();
			
			System.out.println("=================================================");
			System.out.println("Short date;Short index;Close date;Close index;Max loss amount");
			for(int txnIndx=0; txnIndx < buyDates.size(); txnIndx++){
				System.out.println(sellDates.get(txnIndx) + ";" + sellIndex.get(txnIndx) + ";" + 
						buyDates.get(txnIndx) + ";" + buyIndex.get(txnIndx) + ";" + maxLoss[txnIndx]);
			}
			
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
			e.printStackTrace();

		}
	}

}
